package com.cqtczh.qhd.driver.acticity.user;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.OrderInfoBeen;

import tools.L;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.google.gson.reflect.TypeToken;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.IntentionAddressBeen;

public class IntentionAddress extends BaseActivity implements OnClickListener {

	private ImageView head_left;
	private EditText driver_carnumber;
	private ListView addcargo_listmap;
	private SimpleAdapter adapter;
	// private SuggestionSearch mSuggestionSearch;
	private String seachWords;
	private SuggestionSearch mSuggestionSearch;
	private List<Map<String, Object>> list;
	private HashMap<String, String> mapm = null;
	private Button confirm;

	private Button cleanAddress;
	private AppContent mAppContent;
	private TextView intentionAddress;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(AppContent.getContext());
		setContentView(R.layout.activity_intention_address);
		mSuggestionSearch = SuggestionSearch.newInstance();
		mAppContent = (AppContent) getApplication();
		findIds();
		blindOnclick();
		getIntentionAddress();
		OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
			public void onGetSuggestionResult(SuggestionResult res) {
				if (res == null || res.getAllSuggestions() == null) {

					return;
					// 未找到相关结果
				} else {

					initListAdrr(res.getAllSuggestions());

				}
				// 获取在线建议检索结果
			}
		};
		mSuggestionSearch.setOnGetSuggestionResultListener(listener);

	}

	/**
	 * 
	 * @Title: getIntentionAddress
	 * @Description: TODO(这里用一句话描述这个方法的作用) 查询司机已经设置的意向地
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void getIntentionAddress() {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("carownerid", mAppContent.userId);
		HttpJsonTool mHttpJsonTool = new HttpJsonTool(this);
		mHttpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					if (msg.obj != null) {
						List<IntentionAddressBeen> data = (List<IntentionAddressBeen>) msg.obj;
						if (data != null
								&& (!data.isEmpty())
								&& (data.get(0).getIntentionaddress() != null)
								&& (data.get(0).getIntentionaddress().length() > 0)) {
							intentionAddress.setVisibility(View.VISIBLE);
							intentionAddress.setText("已经设置的返空目的地："
									+ data.get(0).getIntentionaddress());
						}
					}
				} else {
					intentionAddress.setVisibility(View.GONE);
				}
			}
		});
		Type type = new TypeToken<List<IntentionAddressBeen>>() {
		}.getType();
		mHttpJsonTool.StartWork(data, GagApi.DRIVER_GET_INTENTION_ADDRESS,
				type, null);

	}

	private List<Map<String, Object>> getData(
			List<SuggestionInfo> allSuggestions) {
		// System.out.println("-----START-----");
		for (SuggestionInfo c : allSuggestions) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			// System.out.println("ket:" + c.key);
			map.put("a0_common", c.key);
			map.put("a1_common", c.city + c.district + c.key);

			list.add(map);
		}
		// System.out.println("-----END-----");

		return list;
	}

	Intent tochangeIntent;
	Bundle bundle;

	private void initListAdrr(List<SuggestionInfo> allSuggestions) {
		list = new ArrayList<Map<String, Object>>();
		adapter = new SimpleAdapter(IntentionAddress.this,
				getData(allSuggestions), R.layout.activity_cargoaddr,
				new String[] { "a0_common", "a1_common" }, new int[] {
						R.id.a0_common, R.id.a1_common });
		addcargo_listmap.setAdapter(adapter);
		addcargo_listmap.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mapm = (HashMap<String, String>) addcargo_listmap
						.getItemAtPosition(position);
				driver_carnumber.setText(mapm.get("a1_common"));
				//
				// L.v("a0_common" + mapm.get("a0_common"));
				// 该字段包含区名字为全名
				// L.v("a1_common=" + mapm.get("a1_common"));

			}
		});
	}

	private void blindOnclick() {
		head_left.setVisibility(0);
		head_left.setOnClickListener(this);
		// driver_carnumber.setOnClickListener(this);
		driver_carnumber.addTextChangedListener(new TextWatcher() {

			@SuppressLint("NewApi")
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

				seachWords = arg0 + "";
				if (null != seachWords || seachWords.isEmpty()) {
					System.out.println(seachWords);
					mSuggestionSearch
							.requestSuggestion((new SuggestionSearchOption())
									.keyword(seachWords).city("重庆市"));
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				// System.out.println(arg0);
			}
		});
		confirm.setOnClickListener(this);

	}

	protected void getmap(String seachWords2) {

	}

	private void findIds() {
		driver_carnumber = (EditText) findViewById(R.id.driver_carnumber);
		head_left = (ImageView) findViewById(R.id.head_left);
		addcargo_listmap = (ListView) findViewById(R.id.addcargo_listmap);
		addcargo_listmap.setDividerHeight(0);
		TextView head_title = (TextView) findViewById(R.id.head_title);
		head_title.setText("设置返空目的地");
		confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(this);
		cleanAddress = (Button) findViewById(R.id.cleanAddress);
		cleanAddress.setOnClickListener(this);
		intentionAddress = (TextView) findViewById(R.id.intentionAddress);
	}

	/**
	 * 设置意向地
	 * 
	 * @Title: setIntentionAddress
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param isClean 参数说明 是否是清楚意向地
	 * @return void 返回类型
	 * @throws
	 */
	private void setIntentionAddress(final boolean isClean) {
		HttpJsonTool mHttpJsonTool = new HttpJsonTool(this);
		mHttpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				Toast.makeText(IntentionAddress.this, (String) msg.obj,
						Toast.LENGTH_LONG).show();
				dismissLoadingDialog();
				if (!isClean) {
					getIntentionAddress();
				} else {
					intentionAddress.setVisibility(View.GONE);
				}
			}
		});
		Map<String, Object> data = new HashMap<String, Object>();

		data.put("carownerid", mAppContent.userId);
		if (!isClean) {
			String address = driver_carnumber.getText().toString();
			if (!TextUtils.isEmpty(address)) {
				data.put("intentionAddress", address);
				mHttpJsonTool.StartWork(data,
						GagApi.DRIVER_SET_INTENTION_ADDRESS, null, null);
				showLoadingDialog("操作中...");
			} else {
				Toast.makeText(this, "请输入意向地", Toast.LENGTH_LONG).show();
			}
		} else {
			data.put("intentionAddress", "");
			mHttpJsonTool.StartWork(data, GagApi.DRIVER_SET_INTENTION_ADDRESS,
					null, null);
			showLoadingDialog("操作中...");
		}

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.confirm:
			L.v("点击了");
			setIntentionAddress(false);
			break;
		case R.id.cleanAddress:
			setIntentionAddress(true);
		default:
			break;
		}
	}

}
